<script setup>
import { useRouter, useRoute } from "vue-router";
import menus from "./router/menu";
import cache from "@/utils/cache";
const router = useRouter();
const route = useRoute();

const goLink = (path) => {
  router.push(path);
};
const center = () => {
  router.push("/user/center");
}
const exit = () => {
  cache.session.remove("token");
  router.push("/login");
}
</script>

<template>
  <router-view v-if="route.fullPath.includes('/preview/') || route.fullPath.includes('/collect/') || route.fullPath.includes('/login')"></router-view>
  <el-container v-else>
    <el-header class="header">
      <el-link type="info" href="/" style="width: 150px;">
        <img style="width: 20%;" src="./assets/img/logo.png" alt="logo" />
        <h2 style="margin-left: 10px;font-weight: bold;color: #555;">小治问卷</h2>
      </el-link>

      <div class="user-box">
        <el-dropdown>
          <span class="el-dropdown-link">
            <el-avatar :size="35" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" style="margin-right: 10px;"></el-avatar>
            <span class="user-name">管理员</span>
          </span>
          <template #dropdown>
            <el-dropdown-menu slot="dropdown">
              <!-- <el-dropdown-item @click="center">个人中心</el-dropdown-item> -->
              <el-dropdown-item @click="exit">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <el-container>
      <el-aside width="200px" class="aside">
        <el-scrollbar>
          <el-menu>
            <template v-for="item in menus" :key="item.path" :index="item.path">
              <el-sub-menu v-if="item.children" :index="item.path">
                <template #title>
                  <component class="icons" :is="item.icon" />
                  <span>{{ item.name }}</span>
                </template>
                <el-menu-item v-for="child in item.children" :key="item.path + child.path" :index="item.path + child.path" @click="goLink(item.path + child.path)">
                  <component class="icons" :is="child.icon" />
                  <span>{{ child.name }}</span>
                </el-menu-item>
              </el-sub-menu>
              <el-menu-item v-else @click="goLink(item.path)">
                <component class="icons" :is="item.icon" />
                <span>{{ item.name }}</span>
              </el-menu-item>
            </template>
          </el-menu>
        </el-scrollbar>
      </el-aside>

      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<style scoped>
.header {
  color: #333;
  text-align: left;
  line-height: 60px;
  font-size: x-large;
  border-bottom: 1px solid #d3dce6;
}
.user-box {
  float: right;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  cursor: pointer;
}
.user-box .user-name {
  vertical-align: 15px;
}
</style>
